<?php

function current_user_can($key, $action = null, $id = null) {
    global $aldb;

    if (empty($key)) {
        return true;
    }

    $user = current_user();
    if (empty($user->id)) {
        return false;
    }

    if ($user->is_superuser) {
        return true;
    }

    /* 文章管理的权限 */
    /*
        如果该用户有管理该分类下所有文章的权限, 则返回true
        如果该用户只有管理自己文章的权限, 查看是否id等于自己, 如果是则返回true;
    
    */
    if ($key == 'Post') {
        if ($action == 'manage') {
            $category_id = $aldb->get_var("SELECT category_id FROM $aldb->post2category WHERE post_id='$id'");
            $poster_id = $aldb->get_var("SELECT poster_id FROM $aldb->posts WHERE id='$id'");
            if (current_user_can('Category', 'manage_all', $category_id)) {
                return true;
            } else if (current_user_can('Category', 'manage_own', $category_id)) {
                if ($poster_id == $user->id) {
                    return true;
                }
            }
            return false;
        } else if ($action == 'add') {
            $key = 'Category';
        }
    }

    if ($key != 'Category' && $key != 'Page') {
        $key_original = $key;
        $key = 'AdminMenu';
    }
    $capability = get_capability($user->group_id, $key);

    if ($key == 'Category') {
        switch ($action) {
            case 'delete':
                if (!empty($capability[$id]['manage_all'])) {
                    return true;
                } else if (!empty($capability[$id]['manage_own']) && $poster_id == $user->id) {
                    return true;
                } else {
                    return false;
                }
                break;
            case 'add':
            case 'manage_own':
                if (!empty($capability[$id]['manage_own']) || !empty($capability[$id]['manage_all'])) {
                    return true;
                } else {
                    return false;
                }
                break;
            case 'manage_all':
                if (!empty($capability[$id]['manage_all'])) {
                    return true;
                } else {
                    return true;
                }
                break;
        }
    }

    if (empty($capability)) {
        return false;
    }

    if (!empty($action) && empty($id)) {
        if (empty($capability[$key_original][$action])) {
            return false;
        } else {
            return true;
        }
    } else if (!empty($action) && !empty($id)) {
        if (empty($capability[$id][$action])) {
            return false;
        } else {
            return true;
        }
    }
}

function current_user() {
    return ArlicleUser::current_user();
}

class ArlicleUser {

    // Gets a reference to the ArlicleUser object instance
    function &get_instance($user_id = 0) {
        static $instance = array();
        if (!isset($instance[0]) || !$instance[0])
            $instance[0] =& new ArlicleUser();
        return $instance[0];
    }

    function get_user() {
        $_this =& ArlicleUser::get_instance();
    }

    function current_user() {
        global $aldb;
        $_this =& ArlicleUser::get_instance();
        if (empty($_this->current_user)) {
            $_this->current_user = null;
            $user_cookie = al_getcookie();
            $username = $user_cookie['username'];
            $password = $user_cookie['password'];
            if (!empty($username) && !empty($password)) {
                $user = $aldb->get_row("SELECT `id`, `password`, `group_id`, `is_superuser` FROM $aldb->users WHERE username='$username'");
                if (!empty($user) && $password == md5($user->password)) {
                    $user->username = $username;
                    $_this->current_user = $user;
                }
            } else {
                $_this->current_user = new AnonymousUser();
            }
        }
        return $_this->current_user;
    }

    function user_id() {
        $_this =& ArlicleUser::get_instance();
        return $_this->current_user->id;
    }

    function username() {
        $_this =& ArlicleUser::get_instance();
        return $_this->current_user->username;
    }

    function group() {
        $_this =& ArlicleUser::get_instance();
        return $_this->current_user->group_id;
    }
}

class AnonymousUser {
    var $id = 0;
    var $username = '';
    var $is_superuser = false;
    var $group_id = 1;
    var $date_joined = '';

    function is_anonymous() {
        return true;
    }
}
?>
